package com.focus.pay.ali.action.order;

import cn.hutool.json.JSONUtil;
import com.alipay.api.AlipayClient;
import com.alipay.api.internal.util.WebUtils;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipayTradeWapPayResponse;
import com.focus.pay.ali.config.AliPayConfig;
import com.focus.pay.ali.constant.Constant;
import com.focus.pay.ali.constant.AliChannelType;
import com.focus.pay.ali.constant.AliVersion;
import com.focus.pay.core.enums.Channel;
import com.focus.pay.ali.model.order.AliPayModelMobile;
import com.focus.pay.core.annation.PayOrderHandler;
import com.focus.pay.core.exception.ExceptionEnum;
import com.focus.pay.core.exception.PayException;

/**
 * @Author: 赵参谋
 * @Date: 2021/10/11 14:37
 * @Description: 支付宝-手机网站支付
 * 适用H5支付
*/
@PayOrderHandler(channel = Channel.AliPay,channelType = AliChannelType.Mobile,version = AliVersion.VERSION_2)
public class AliPayOrderMobile extends AliPayOrder {


    public AlipayTradeWapPayResponse payOrder(AliPayModelMobile aliPayModelMobile, AliPayConfig aliPayConfig) throws PayException {
        try{
            AlipayClient alipayClient = Constant.buildAlipayClient(aliPayConfig);
            AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest  ();
            request.setNotifyUrl(aliPayModelMobile.getNotifyUrl());
            WebUtils.setNeedCheckServerTrusted(false);
            request.setBizContent(JSONUtil.toJsonStr(aliPayModelMobile));
            AlipayTradeWapPayResponse  response = alipayClient.pageExecute(request);
            return response;
        }catch (Exception e){
            throw new PayException(ExceptionEnum.ORDER_EXCEPTION);
        }
    }
}
